/*************************************************************************

            (c) Copyright 2019 by 天翼物联科技有限公司. All rights reserved.

*************************************************************************/
#include "ctiot_demo.h"
#include "ctiot_log.h"
#include "ctiot_mqtt_sdk_tmodel.h"
#include <sys/time.h>
#include <time.h>

#define CTIOT_MQTT_ID "15133430zt-01"
#define CTIOT_MQTT_TOKEN "MKDBTuIcdGcj7gYDPIkypgU_PFNTc-ecWOBISx-YIVU"

#define CTIOT_MQTT_TCPADDRESS "mqtt.ctwing.cn"
#define CTIOT_MQTT_TCPPORT 1883
#define CTIOT_MQTT_TLSADDRESS "mqtt.ctwing.cn"
#define CTIOT_MQTT_TLSPORT 8883

#define MAX_CNT 9000000000
#define CTIOT_DEMO_LOG CTIOT_LOG

mqtt_param_s mqttPara;

uint8_t mqttReportFlag = 1;
uint16_t mqttReportTime = 5;
uint32_t mqttSendCount = 0;
uint32_t mqttRecvCount = 0;
uint32_t mqttSendErrorCount = 0;
uint32_t CtiotMqttGetCnt(uint32_t cntType) {
	uint32_t retCnt = 0;
	switch (cntType) {
	case CTIOT_MQTT_REPORT_CNT:
		mqttSendCount = (mqttSendCount == MAX_CNT) ? 1 : (mqttSendCount + 1);
		retCnt = mqttSendCount;
		break;
	case CTIOT_MQTT_RECV_CNT:
		mqttRecvCount = (mqttRecvCount == MAX_CNT) ? 1 : (mqttRecvCount + 1);
		retCnt = mqttRecvCount;
		break;
	case CTIOT_MQTT_REPORT_ERROR_CNT:
		mqttSendErrorCount =
		(mqttSendErrorCount == MAX_CNT) ? 1 : (mqttSendErrorCount + 1);
		retCnt = mqttSendErrorCount;
		break;

	default:
		CTIOT_DEMO_LOG(LOG_DEBUG, "not count!");
	break;
	}

	CTIOT_DEMO_LOG(LOG_DEBUG,"mqttSendCount=[ %d ] mqttRecvCount=[ %d ] mqttSendErrorCount=[ %d ]",mqttSendCount, mqttRecvCount, mqttSendErrorCount);
	return retCnt;
}

static uint8_t CtiotTimer0(uint8_t reportFlag, uint16_t reportTime) {
	uint8_t ret = 0;
	uint8_t flag = reportFlag;
	uint16_t time = reportTime;
	static struct timeval lastTime = { 0, },now = { 0, };
	if (flag == 1) {
		gettimeofday(&now, NULL);
		if (lastTime.tv_sec != 0) {
			if (now.tv_sec - lastTime.tv_sec >= time) {
				lastTime.tv_sec = now.tv_sec;
				ret = 1;
			} else {
				ret = 0;
			}
		} else {
			lastTime.tv_sec = now.tv_sec;
			ret = 1;
		}
	}

	return ret;
}

void CtiotYield(void) {}

static void CtiotMqttExecuteDataReport(void)
{
	ctiot_mqtt_msg_s ptrMsg = {0};
	CTIOT_RESULT_CODE ret = CTIOT_SUCCESS;
	DataReportS para = { 0 };
	para.temperature_data = 100.0;
	para.humidity_data = 100;
#if DATA_MODE == 3
	ret = CtiotMqttSendDataReportData(&para, MQTT_QOS_ONLY_ONCE, &ptrMsg);
#elif DATA_MODE == 2
	char *payload = NULL;
	ret = CtiotMqttEncodeDataReport(&para, &payload);
	if (ret == CTIOT_SUCCESS)
	{
		ret = ctiot_mqtt_send_data("data_report", MQTT_QOS_ONLY_ONCE, payload,&ptrMsg);
	}
	CtiotTmodelFree(payload);
#endif

	if (ret == CTIOT_SUCCESS) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendDataReportData: SUCCESS, msgID: %u! ", ptrMsg.msgID);
	} else if (ret == CTIOT_PARA_ERROR || ret == CTIOT_ENCODE_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendDataReportData: Pls check the para! ");
	} else if (ret == CTIOT_BUF_OVERFLOW) {
		CTIOT_DEMO_LOG(LOG_INFO, "CtiotMqttSendDataReportData: Pls check the payload size,ref @MQTT_SENDBUF_SIZE! ");
	} else if (ret == CTIOT_QUEUE_FULL) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendDataReportData: queue full,ref @MAX_MSG_COUNT ! ");
	} else if (ret == CTIOT_SEND_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendDataReportData: send error Fatal!! will receive CTIOT_EVENT_CLIENT_NOTLOGIN event !");
	} else if (ret == CTIOT_SES_NOK_NOTINITIAL) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendDataReportData: Pls initial First,then retry! ");
		return;
	} else if (ret == CTIOT_SES_NOK_NOTLOGIN) {
		CTIOT_DEMO_LOG(LOG_INFO, "CtiotMqttSendDataReportData: Pls login !");
	} else if (ret == CTIOT_SES_NOK_LOGINING || ret == CTIOT_SES_NOK_LOGOUTING) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendDataReportData: Pls wait a moment,then retry! ");
	} else if (ret == CTIOT_MUTEX_INIT_ERROR || ret == CTIOT_MUTEX_LOCK_ERROR || ret == CTIOT_MUTEX_UNLOCK_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendDataReportData: MUTEX Fatal!! SDK will Logout,Pls check the sdk status and Login!");
		return;
	} else if (ret == CTIOT_MALLOC_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendDataReportData: Pls check the heap size! ");
		return;
	} else {
		return;
	}
	
	if (ret == CTIOT_SUCCESS) {
		CtiotMqttGetCnt(CTIOT_MQTT_REPORT_CNT);
	} else {
		CtiotMqttGetCnt(CTIOT_MQTT_REPORT_ERROR_CNT);
	}
}
static void CtiotMqttExecuteAirConditionerOnoffResp(uint32_t taskId)
{
	ctiot_mqtt_msg_s ptrMsg = {0};
	CTIOT_RESULT_CODE ret = CTIOT_SUCCESS;
	AirConditionerOnoffRespS para = { 0 };
	para.taskId = taskId;
	para.air_conditioner_onoff = 0;
	para.act_result = 0;
#if DATA_MODE == 3
	ret = CtiotMqttSendAirConditionerOnoffRespData(&para, MQTT_QOS_ONLY_ONCE, &ptrMsg);
#elif DATA_MODE == 2
	char *payload = NULL;
	ret = CtiotMqttEncodeAirConditionerOnoffResp(&para, &payload);
	if (ret == CTIOT_SUCCESS)
	{
		ret = ctiot_mqtt_send_data("air_conditioner_onoff_resp", MQTT_QOS_ONLY_ONCE, payload,&ptrMsg);
	}
	CtiotTmodelFree(payload);
#endif

	if (ret == CTIOT_SUCCESS) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendAirConditionerOnoffRespData: SUCCESS, msgID: %u! ", ptrMsg.msgID);
	} else if (ret == CTIOT_PARA_ERROR || ret == CTIOT_ENCODE_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendAirConditionerOnoffRespData: Pls check the para! ");
	} else if (ret == CTIOT_BUF_OVERFLOW) {
		CTIOT_DEMO_LOG(LOG_INFO, "CtiotMqttSendAirConditionerOnoffRespData: Pls check the payload size,ref @MQTT_SENDBUF_SIZE! ");
	} else if (ret == CTIOT_QUEUE_FULL) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendAirConditionerOnoffRespData: queue full,ref @MAX_MSG_COUNT ! ");
	} else if (ret == CTIOT_SEND_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendAirConditionerOnoffRespData: send error Fatal!! will receive CTIOT_EVENT_CLIENT_NOTLOGIN event !");
	} else if (ret == CTIOT_SES_NOK_NOTINITIAL) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendAirConditionerOnoffRespData: Pls initial First,then retry! ");
		return;
	} else if (ret == CTIOT_SES_NOK_NOTLOGIN) {
		CTIOT_DEMO_LOG(LOG_INFO, "CtiotMqttSendAirConditionerOnoffRespData: Pls login !");
	} else if (ret == CTIOT_SES_NOK_LOGINING || ret == CTIOT_SES_NOK_LOGOUTING) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendAirConditionerOnoffRespData: Pls wait a moment,then retry! ");
	} else if (ret == CTIOT_MUTEX_INIT_ERROR || ret == CTIOT_MUTEX_LOCK_ERROR || ret == CTIOT_MUTEX_UNLOCK_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendAirConditionerOnoffRespData: MUTEX Fatal!! SDK will Logout,Pls check the sdk status and Login!");
		return;
	} else if (ret == CTIOT_MALLOC_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendAirConditionerOnoffRespData: Pls check the heap size! ");
		return;
	} else {
		return;
	}
	
	if (ret == CTIOT_SUCCESS) {
		CtiotMqttGetCnt(CTIOT_MQTT_REPORT_CNT);
	} else {
		CtiotMqttGetCnt(CTIOT_MQTT_REPORT_ERROR_CNT);
	}
}
static void CtiotMqttExecuteIrSensorReport(void)
{
	ctiot_mqtt_msg_s ptrMsg = {0};
	CTIOT_RESULT_CODE ret = CTIOT_SUCCESS;
	IrSensorReportS para = { 0 };
	para.ir_sensor_data = 0;
#if DATA_MODE == 3
	ret = CtiotMqttSendIrSensorReportData(&para, MQTT_QOS_ONLY_ONCE, &ptrMsg);
#elif DATA_MODE == 2
	char *payload = NULL;
	ret = CtiotMqttEncodeIrSensorReport(&para, &payload);
	if (ret == CTIOT_SUCCESS)
	{
		ret = ctiot_mqtt_send_data("ir_sensor_report", MQTT_QOS_ONLY_ONCE, payload,&ptrMsg);
	}
	CtiotTmodelFree(payload);
#endif

	if (ret == CTIOT_SUCCESS) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendIrSensorReportData: SUCCESS, msgID: %u! ", ptrMsg.msgID);
	} else if (ret == CTIOT_PARA_ERROR || ret == CTIOT_ENCODE_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendIrSensorReportData: Pls check the para! ");
	} else if (ret == CTIOT_BUF_OVERFLOW) {
		CTIOT_DEMO_LOG(LOG_INFO, "CtiotMqttSendIrSensorReportData: Pls check the payload size,ref @MQTT_SENDBUF_SIZE! ");
	} else if (ret == CTIOT_QUEUE_FULL) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendIrSensorReportData: queue full,ref @MAX_MSG_COUNT ! ");
	} else if (ret == CTIOT_SEND_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendIrSensorReportData: send error Fatal!! will receive CTIOT_EVENT_CLIENT_NOTLOGIN event !");
	} else if (ret == CTIOT_SES_NOK_NOTINITIAL) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendIrSensorReportData: Pls initial First,then retry! ");
		return;
	} else if (ret == CTIOT_SES_NOK_NOTLOGIN) {
		CTIOT_DEMO_LOG(LOG_INFO, "CtiotMqttSendIrSensorReportData: Pls login !");
	} else if (ret == CTIOT_SES_NOK_LOGINING || ret == CTIOT_SES_NOK_LOGOUTING) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendIrSensorReportData: Pls wait a moment,then retry! ");
	} else if (ret == CTIOT_MUTEX_INIT_ERROR || ret == CTIOT_MUTEX_LOCK_ERROR || ret == CTIOT_MUTEX_UNLOCK_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendIrSensorReportData: MUTEX Fatal!! SDK will Logout,Pls check the sdk status and Login!");
		return;
	} else if (ret == CTIOT_MALLOC_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendIrSensorReportData: Pls check the heap size! ");
		return;
	} else {
		return;
	}
	
	if (ret == CTIOT_SUCCESS) {
		CtiotMqttGetCnt(CTIOT_MQTT_REPORT_CNT);
	} else {
		CtiotMqttGetCnt(CTIOT_MQTT_REPORT_ERROR_CNT);
	}
}
static void CtiotMqttExecuteInfoReport(void)
{
	ctiot_mqtt_msg_s ptrMsg = {0};
	CTIOT_RESULT_CODE ret = CTIOT_SUCCESS;
	InfoReportS para = { 0 };
	para.hardware_version = "demo";
	para.software_version = "demo";
#if DATA_MODE == 3
	ret = CtiotMqttSendInfoReportData(&para, MQTT_QOS_ONLY_ONCE, &ptrMsg);
#elif DATA_MODE == 2
	char *payload = NULL;
	ret = CtiotMqttEncodeInfoReport(&para, &payload);
	if (ret == CTIOT_SUCCESS)
	{
		ret = ctiot_mqtt_send_data("info_report", MQTT_QOS_ONLY_ONCE, payload,&ptrMsg);
	}
	CtiotTmodelFree(payload);
#endif

	if (ret == CTIOT_SUCCESS) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendInfoReportData: SUCCESS, msgID: %u! ", ptrMsg.msgID);
	} else if (ret == CTIOT_PARA_ERROR || ret == CTIOT_ENCODE_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendInfoReportData: Pls check the para! ");
	} else if (ret == CTIOT_BUF_OVERFLOW) {
		CTIOT_DEMO_LOG(LOG_INFO, "CtiotMqttSendInfoReportData: Pls check the payload size,ref @MQTT_SENDBUF_SIZE! ");
	} else if (ret == CTIOT_QUEUE_FULL) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendInfoReportData: queue full,ref @MAX_MSG_COUNT ! ");
	} else if (ret == CTIOT_SEND_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendInfoReportData: send error Fatal!! will receive CTIOT_EVENT_CLIENT_NOTLOGIN event !");
	} else if (ret == CTIOT_SES_NOK_NOTINITIAL) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendInfoReportData: Pls initial First,then retry! ");
		return;
	} else if (ret == CTIOT_SES_NOK_NOTLOGIN) {
		CTIOT_DEMO_LOG(LOG_INFO, "CtiotMqttSendInfoReportData: Pls login !");
	} else if (ret == CTIOT_SES_NOK_LOGINING || ret == CTIOT_SES_NOK_LOGOUTING) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendInfoReportData: Pls wait a moment,then retry! ");
	} else if (ret == CTIOT_MUTEX_INIT_ERROR || ret == CTIOT_MUTEX_LOCK_ERROR || ret == CTIOT_MUTEX_UNLOCK_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendInfoReportData: MUTEX Fatal!! SDK will Logout,Pls check the sdk status and Login!");
		return;
	} else if (ret == CTIOT_MALLOC_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendInfoReportData: Pls check the heap size! ");
		return;
	} else {
		return;
	}
	
	if (ret == CTIOT_SUCCESS) {
		CtiotMqttGetCnt(CTIOT_MQTT_REPORT_CNT);
	} else {
		CtiotMqttGetCnt(CTIOT_MQTT_REPORT_ERROR_CNT);
	}
}
static void CtiotMqttExecuteSetAutoControlResp(uint32_t taskId)
{
	ctiot_mqtt_msg_s ptrMsg = {0};
	CTIOT_RESULT_CODE ret = CTIOT_SUCCESS;
	SetAutoControlRespS para = { 0 };
	para.taskId = taskId;
	para.auto_control = 0;
	para.act_result = 0;
#if DATA_MODE == 3
	ret = CtiotMqttSendSetAutoControlRespData(&para, MQTT_QOS_ONLY_ONCE, &ptrMsg);
#elif DATA_MODE == 2
	char *payload = NULL;
	ret = CtiotMqttEncodeSetAutoControlResp(&para, &payload);
	if (ret == CTIOT_SUCCESS)
	{
		ret = ctiot_mqtt_send_data("set_auto_control_resp", MQTT_QOS_ONLY_ONCE, payload,&ptrMsg);
	}
	CtiotTmodelFree(payload);
#endif

	if (ret == CTIOT_SUCCESS) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendSetAutoControlRespData: SUCCESS, msgID: %u! ", ptrMsg.msgID);
	} else if (ret == CTIOT_PARA_ERROR || ret == CTIOT_ENCODE_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendSetAutoControlRespData: Pls check the para! ");
	} else if (ret == CTIOT_BUF_OVERFLOW) {
		CTIOT_DEMO_LOG(LOG_INFO, "CtiotMqttSendSetAutoControlRespData: Pls check the payload size,ref @MQTT_SENDBUF_SIZE! ");
	} else if (ret == CTIOT_QUEUE_FULL) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendSetAutoControlRespData: queue full,ref @MAX_MSG_COUNT ! ");
	} else if (ret == CTIOT_SEND_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendSetAutoControlRespData: send error Fatal!! will receive CTIOT_EVENT_CLIENT_NOTLOGIN event !");
	} else if (ret == CTIOT_SES_NOK_NOTINITIAL) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendSetAutoControlRespData: Pls initial First,then retry! ");
		return;
	} else if (ret == CTIOT_SES_NOK_NOTLOGIN) {
		CTIOT_DEMO_LOG(LOG_INFO, "CtiotMqttSendSetAutoControlRespData: Pls login !");
	} else if (ret == CTIOT_SES_NOK_LOGINING || ret == CTIOT_SES_NOK_LOGOUTING) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendSetAutoControlRespData: Pls wait a moment,then retry! ");
	} else if (ret == CTIOT_MUTEX_INIT_ERROR || ret == CTIOT_MUTEX_LOCK_ERROR || ret == CTIOT_MUTEX_UNLOCK_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendSetAutoControlRespData: MUTEX Fatal!! SDK will Logout,Pls check the sdk status and Login!");
		return;
	} else if (ret == CTIOT_MALLOC_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendSetAutoControlRespData: Pls check the heap size! ");
		return;
	} else {
		return;
	}
	
	if (ret == CTIOT_SUCCESS) {
		CtiotMqttGetCnt(CTIOT_MQTT_REPORT_CNT);
	} else {
		CtiotMqttGetCnt(CTIOT_MQTT_REPORT_ERROR_CNT);
	}
}
static void CtiotMqttExecuteSetReportPeriodResp(uint32_t taskId)
{
	ctiot_mqtt_msg_s ptrMsg = {0};
	CTIOT_RESULT_CODE ret = CTIOT_SUCCESS;
	SetReportPeriodRespS para = { 0 };
	para.taskId = taskId;
	para.report_period = 86400;
	para.act_result = 0;
#if DATA_MODE == 3
	ret = CtiotMqttSendSetReportPeriodRespData(&para, MQTT_QOS_ONLY_ONCE, &ptrMsg);
#elif DATA_MODE == 2
	char *payload = NULL;
	ret = CtiotMqttEncodeSetReportPeriodResp(&para, &payload);
	if (ret == CTIOT_SUCCESS)
	{
		ret = ctiot_mqtt_send_data("set_report_period_resp", MQTT_QOS_ONLY_ONCE, payload,&ptrMsg);
	}
	CtiotTmodelFree(payload);
#endif

	if (ret == CTIOT_SUCCESS) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendSetReportPeriodRespData: SUCCESS, msgID: %u! ", ptrMsg.msgID);
	} else if (ret == CTIOT_PARA_ERROR || ret == CTIOT_ENCODE_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendSetReportPeriodRespData: Pls check the para! ");
	} else if (ret == CTIOT_BUF_OVERFLOW) {
		CTIOT_DEMO_LOG(LOG_INFO, "CtiotMqttSendSetReportPeriodRespData: Pls check the payload size,ref @MQTT_SENDBUF_SIZE! ");
	} else if (ret == CTIOT_QUEUE_FULL) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendSetReportPeriodRespData: queue full,ref @MAX_MSG_COUNT ! ");
	} else if (ret == CTIOT_SEND_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendSetReportPeriodRespData: send error Fatal!! will receive CTIOT_EVENT_CLIENT_NOTLOGIN event !");
	} else if (ret == CTIOT_SES_NOK_NOTINITIAL) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendSetReportPeriodRespData: Pls initial First,then retry! ");
		return;
	} else if (ret == CTIOT_SES_NOK_NOTLOGIN) {
		CTIOT_DEMO_LOG(LOG_INFO, "CtiotMqttSendSetReportPeriodRespData: Pls login !");
	} else if (ret == CTIOT_SES_NOK_LOGINING || ret == CTIOT_SES_NOK_LOGOUTING) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendSetReportPeriodRespData: Pls wait a moment,then retry! ");
	} else if (ret == CTIOT_MUTEX_INIT_ERROR || ret == CTIOT_MUTEX_LOCK_ERROR || ret == CTIOT_MUTEX_UNLOCK_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendSetReportPeriodRespData: MUTEX Fatal!! SDK will Logout,Pls check the sdk status and Login!");
		return;
	} else if (ret == CTIOT_MALLOC_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttSendSetReportPeriodRespData: Pls check the heap size! ");
		return;
	} else {
		return;
	}
	
	if (ret == CTIOT_SUCCESS) {
		CtiotMqttGetCnt(CTIOT_MQTT_REPORT_CNT);
	} else {
		CtiotMqttGetCnt(CTIOT_MQTT_REPORT_ERROR_CNT);
	}
}
#if DATA_MODE == 2
static void CtiotMqttSetReportPeriodCallback(char *data)
#elif DATA_MODE == 3
static void CtiotMqttSetReportPeriodCallback(SetReportPeriodS* para)
#endif
{
	CtiotMqttGetCnt(CTIOT_MQTT_RECV_CNT);
	#if DATA_MODE == 2
	SetReportPeriodS tmp_para = { 0 },* para = &tmp_para;
	CtiotMqttDecodeSetReportPeriodPara(data,para);
	#endif
	//todo:添加同步应答响应,应答taskId应和para->taskId相对应
	
}
#if DATA_MODE == 2
static void CtiotMqttAirConditionerOnoffCmdCallback(char *data)
#elif DATA_MODE == 3
static void CtiotMqttAirConditionerOnoffCmdCallback(AirConditionerOnoffCmdS* para)
#endif
{
	CtiotMqttGetCnt(CTIOT_MQTT_RECV_CNT);
	#if DATA_MODE == 2
	AirConditionerOnoffCmdS tmp_para = { 0 },* para = &tmp_para;
	CtiotMqttDecodeAirConditionerOnoffCmdPara(data,para);
	#endif
	//todo:添加同步应答响应,应答taskId应和para->taskId相对应
	
}
#if DATA_MODE == 2
static void CtiotMqttSetAutoControlCallback(char *data)
#elif DATA_MODE == 3
static void CtiotMqttSetAutoControlCallback(SetAutoControlS* para)
#endif
{
	CtiotMqttGetCnt(CTIOT_MQTT_RECV_CNT);
	#if DATA_MODE == 2
	SetAutoControlS tmp_para = { 0 },* para = &tmp_para;
	CtiotMqttDecodeSetAutoControlPara(data,para);
	#endif
	//todo:添加同步应答响应,应答taskId应和para->taskId相对应
	
}
CtiotSrvCbS srvCB = {
	CtiotMqttSetReportPeriodCallback,
	CtiotMqttAirConditionerOnoffCmdCallback,
	CtiotMqttSetAutoControlCallback,
};
void testAsyncMsgStatus(uint32_t msgID, int8_t status) 
{
	CTIOT_DEMO_LOG(LOG_INFO, "testAsyncMsgStatus msgID: %d , status: %d ", msgID, status);

	if (status == CTIOT_ASYNC_MSG_MQTT_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"testAsyncMsgStatus: CTIOT_CLIENT had Logout, Pls login !");
	}
}
void CtiotMqttEventNotify(int8_t status) 
{
	if (status == CTIOT_EVENT_CLIENT_NOTLOGIN) {
		CTIOT_DEMO_LOG(LOG_INFO,"CtiotMqttEventNotify: CTIOT_CLIENT had Logout, Pls login !");
	}
}
void CtiotSendData(void) {
	if (1 == CtiotTimer0(mqttReportFlag, mqttReportTime)) {
		CtiotMqttExecuteDataReport();
		CtiotMqttExecuteIrSensorReport();
		CtiotMqttExecuteInfoReport();
		/* 以下是下行指令响应代码，在处理下行指令时应选择正确的响应
		para->taskId参数是每个下行指令数据结构中的一个数据项，响应的taskId应和指令的taskId保持一致
		CtiotMqttExecuteAirConditionerOnoffResp(para->taskId);
		CtiotMqttExecuteSetAutoControlResp(para->taskId);
		CtiotMqttExecuteSetReportPeriodResp(para->taskId);
		*/
	}
}
void CtiotMqttEntry(void) {
	CTIOT_RESULT_CODE ret = CTIOT_SUCCESS;

	mqttPara.serverIp = CTIOT_MQTT_TCPADDRESS;
	mqttPara.serverPort = CTIOT_MQTT_TCPPORT;
	mqttPara.password = CTIOT_MQTT_TOKEN;
	mqttPara.deviceId = CTIOT_MQTT_ID;
	mqttPara.connect_mode = 0;
	mqttPara.msgStatusCallback = testAsyncMsgStatus;
	mqttPara.eventStatusCallback = CtiotMqttEventNotify;

	ret = ctiot_mqtt_init(&mqttPara, &srvCB);

	CTIOT_DEMO_LOG(LOG_INFO, "ctiot_mqtt_init: ret= %d ", ret);

	if (ret == CTIOT_SUCCESS) {
		CTIOT_DEMO_LOG(LOG_INFO, "ctiot_mqtt_init: SUCCESS! ");
	} else if (ret == CTIOT_PARA_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO, "ctiot_mqtt_init: Pls check the para! ");
		return;
	} else if (ret == CTIOT_SES_NOK_LOGINED) {
		CTIOT_DEMO_LOG(LOG_INFO, "ctiot_mqtt_init: Pls Logout First,then retry! ");
		return;
	} else if (ret == CTIOT_SES_NOK_LOGINING || ret == CTIOT_SES_NOK_LOGOUTING) {
		CTIOT_DEMO_LOG(LOG_INFO, "ctiot_mqtt_init: Pls wait a moment,then retry! ");
		return;
	} else if (ret == CTIOT_MALLOC_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO, "ctiot_mqtt_init: Pls check the heap size! ");
		return;
	} else {
		return;
	}

relogin:
	CTIOT_DEMO_LOG(LOG_INFO, "deviceID: %s , password: %s ", mqttPara.deviceId,mqttPara.password);

	ret = ctiot_mqtt_login(5);

	CTIOT_DEMO_LOG(LOG_INFO, "ctiot_mqtt_login: ret= %d ", ret);

	if (ret == CTIOT_SUCCESS) {
		CTIOT_DEMO_LOG(LOG_INFO, "ctiot_mqtt_login: SUCCESS! ");
	} else if (ret == CTIOT_LOGIN_TIMEOUT || ret == CTIOT_NET_CONNECT_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO,"ctiot_mqtt_login: Pls check the net,then retry! ");
	} else if (ret == CTIOT_SES_NOK_NOTINITIAL) {
		CTIOT_DEMO_LOG(LOG_INFO,"ctiot_mqtt_login: Pls initial First,then retry! ");
		return;
	} else if (ret == CTIOT_SES_NOK_LOGINED) {
		CTIOT_DEMO_LOG(LOG_INFO, "ctiot_mqtt_login: Already login ");
	} else if (ret == CTIOT_SES_NOK_LOGOUTING) {
		CTIOT_DEMO_LOG(LOG_INFO,"ctiot_mqtt_login: Pls wait a moment,then retry! ");
	} else if (ret == CTIOT_MQTT_CONNECT_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO, "ctiot_mqtt_login: MQTT Engine Fatal error!! Pls check the net,then retry !");
		return;
	} else if (ret == CTIOT_MQTT_UNNACCEPTABLE_PROTOCOL) {
		CTIOT_DEMO_LOG(LOG_INFO, "ctiot_mqtt_login: MQTT protocol error! Pls check the mqtt net,then retry !");
		return;
	} else if (ret == CTIOT_MQTT_CLIENTID_REJECTED) {
		CTIOT_DEMO_LOG(LOG_INFO, "ctiot_mqtt_login: Client ID reject!! Pls check the mqtt net,then retry !");
		return;
	} else if (ret == CTIOT_MQTT_SERVER_UNAVAILABLE) {
		CTIOT_DEMO_LOG(LOG_INFO, "ctiot_mqtt_login: MQTT Server Unavailable, Pls check the Server,then retry !");
		return;
	} else if (ret == CTIOT_MQTT_BAD_USERNAME_OR_PASSWORD) {
		CTIOT_DEMO_LOG(LOG_INFO,"ctiot_mqtt_login:  Pls check the ClientID or Token,then retry !");
		return;
	} else if (ret == CTIOT_MQTT_NOT_AUTHORIZED) {
		CTIOT_DEMO_LOG(LOG_INFO,"ctiot_mqtt_login: Pls check the ClientID or Token,then retry !");
		return;
	} else if (ret == CTIOT_MUTEX_INIT_ERROR || ret == CTIOT_MUTEX_LOCK_ERROR || ret == CTIOT_MUTEX_UNLOCK_ERROR ||ret == CTIOT_THREAD_CREATE_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO, "ctiot_mqtt_login: MUTEX error!! check threat !");
		return;
	} else if (ret == CTIOT_TLS_ROOT_CERTIFICATE_ERROR ||ret == CTIOT_TLS_CLIENT_CERTIFICATE_ERROR ||ret == CTIOT_TLS_CONFIG_DEFAULTS_ERROR || ret == CTIOT_TLS_CONF_OWN_CERT_ERROR ||ret == CTIOT_TLS_SETUP_ERROR ||ret == CTIOT_TLS_SET_HOSTNAME_ERROR ||ret == CTIOT_TLS_HANDSHAKE_ERROR) {
		CTIOT_DEMO_LOG(LOG_INFO, "ctiot_mqtt_login: Pls check the TLS param,then retry");
		return;
	} else {
		return;
	}

	for (;;) {
		CtiotYield();

		ret = ctiot_mqtt_get_status();

		switch (ret) {
		case CTIOT_CLIENT_NOT_LOGIN:
			CTIOT_DEMO_LOG(LOG_FATAL, "CtiotMqttEntry relogin!!");
			goto relogin;
		case CTIOT_CLIENT_LOGINED:
		{
			CtiotSendData();
			break;
		}
		default:
			break;
		}
	}
}
